package com.webapp.api;

import java.io.IOException;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import com.alibaba.fastjson.JSONObject;
import com.webapp.model.AppInfo;
import com.webapp.service.AppInfoService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.webapp.helper.StringUtil;
import com.webapp.helper.WechatData;
import com.webapp.helper.WechatUtil;


@Controller
public class PageController extends BaseController{

	@Autowired
	private AppInfoService appInfoService;
	
	
	private static final Logger LOGGER = LoggerFactory.getLogger(PageController.class);

	@RequestMapping(value = "redirect")
	public ModelAndView redirect(ModelAndView mv,String code,String streetNo, String state,HttpServletRequest req) {
		String openid = "",appId = WechatData.APP_ID,appSecret = WechatData.APP_SECRET;
		try {
			if(StringUtil.isNotBlank(streetNo)){
				AppInfo appInfo = appInfoService.findByStreetNo(streetNo);
				LOGGER.info("{},{}", JSONObject.toJSONString(appInfo),streetNo);
				if(appInfo!=null){
					appId = appInfo.getAppId();
					appSecret = appInfo.getAppSecret();
				}
			}
			LOGGER.warn("停车场微信 code:{},{},{}",code,state,streetNo);
			if(StringUtil.isNotBlank(code)) {
				openid = WechatUtil.getOpenId(appId,appSecret, code);
			}else {
				LOGGER.info("客户端IP=======> :{}",getClientIp(req));
			}
		} catch (Exception e) {
			LOGGER.error("e:{}",e);
		}
		mv.addObject("openid",openid);
		mv.setViewName("/front/index");
		return mv;
	}
	//道路
	@RequestMapping(value = "redirect1")
	public ModelAndView redirect1(ModelAndView mv,String code,String state,String streetNo,HttpServletRequest req) {
		String openid = "",appId = WechatData.APP_ID,appSecret = WechatData.APP_SECRET;
		try {
			if(StringUtil.isNotBlank(streetNo)){
				AppInfo appInfo = appInfoService.findByStreetNo(streetNo);
				if(appInfo!=null){
					appId = appInfo.getAppId();
					appSecret = appInfo.getAppSecret();
				}
			}
			LOGGER.warn("道路停车，微信 code:{},{}",code,state);
			if(StringUtil.isNotBlank(code)) {
				openid = WechatUtil.getOpenId(appId,appSecret, code);
			}else {
				LOGGER.info("客户端IP=======> :{}",getClientIp(req));
			}
		} catch (Exception e) {
			LOGGER.error("e:{}",e);
		}
		mv.addObject("openid",openid);
		mv.setViewName("/front/road/r_index");
		return mv;
	}
	//包月
	@RequestMapping(value = "redirect2")
	public ModelAndView redirect2(ModelAndView mv,String code,String state,String streetNo,HttpServletRequest req) {
		String openid = "",appId = WechatData.APP_ID,appSecret = WechatData.APP_SECRET;
		try {
			if(StringUtil.isNotBlank(streetNo)){
				AppInfo appInfo = appInfoService.findByStreetNo(streetNo);
				if(appInfo!=null){
					appId = appInfo.getAppId();
					appSecret = appInfo.getAppSecret();
				}
			}
			LOGGER.warn("包月支付，微信:{},{}",code,state);
			if(StringUtil.isNotBlank(code)) {
				openid = WechatUtil.getOpenId(appId,appSecret, code);
			}else {
				LOGGER.info("客户端IP=======> :{}",getClientIp(req));
			}
		} catch (Exception e) {
			LOGGER.error("e:{}",e);
		}
		mv.addObject("openid",openid);
		mv.setViewName("/front/bycp");
		return mv;
	}

	@RequestMapping(value = "free")
	public ModelAndView free(ModelAndView mv,String code,String streetNo,HttpServletRequest req) {
		String openid = "",appId = WechatData.APP_ID,appSecret = WechatData.APP_SECRET;
		try {
			if(StringUtil.isNotBlank(streetNo)){
				AppInfo appInfo = appInfoService.findByStreetNo(streetNo);
				if(appInfo!=null){
					appId = appInfo.getAppId();
					appSecret = appInfo.getAppSecret();
				}
			}
			LOGGER.warn("免单车微信:{},{},{},{}",code,streetNo);
			if(StringUtil.isNotBlank(code)) {
				openid = WechatUtil.getOpenId(appId,appSecret, code);
			}else {
				LOGGER.info("客户端IP=======> :{}",getClientIp(req));
			}
		} catch (Exception e) {
			LOGGER.error("e:{}",e);
		}
		mv.addObject("openid",openid);
		mv.addObject("streetNo",streetNo);
		mv.setViewName("/front/free");
		return mv;
	}

	@RequestMapping(value = "noPlate")
	public ModelAndView noPlate(ModelAndView mv,String code,String status,String streetNo,String gateNo,HttpServletRequest req) {
		String openid = "",appId = WechatData.APP_ID,appSecret = WechatData.APP_SECRET;
		try {
			if(StringUtil.isNotBlank(streetNo)){
				AppInfo appInfo = appInfoService.findByStreetNo(streetNo);
				if(appInfo!=null){
					appId = appInfo.getAppId();
					appSecret = appInfo.getAppSecret();
				}
			}
			LOGGER.warn("无牌车:{},{},{},{}",code,status,streetNo,gateNo);
			if(StringUtil.isNotBlank(code)) {
				openid = WechatUtil.getOpenId(appId,appSecret, code);
			}else {
				LOGGER.info("客户端IP=======> :{}",getClientIp(req));
			}
		} catch (Exception e) {
			LOGGER.error("e:{}",e);
		}
		mv.addObject("openid",openid);
		mv.addObject("streetNo",streetNo);
		mv.addObject("gateNo",gateNo);
		mv.setViewName("/front/noPlate");
		return mv;
	}
	
	/**
	 * 停车场须知
	 * @return
	 */
	@RequestMapping(value = "/notice")
	public String notice() {
		return "/front/notice";
	}
	
	@RequestMapping(path = "page")
	public String ppp(HttpServletRequest request){
		try {
			String json = getRequestJsonString(request);
			LOGGER.warn("点金计划 msg:{}",json);
		} catch (IOException e) {
			e.printStackTrace();
		}
		return "/front/page";
	}
	
	/**
	 * 停车记录
	 * @return
	 */
	@RequestMapping(value = "logs")
	public String logs() {
		return "/front/logs";
	}
	
	/**
	 * 停车详情
	 * @return
	 */
	@RequestMapping(value = "detail")
	public String detail() {
		return "/front/detail";
	}
	
	@RequestMapping(value = "coupon")
	public String coupon() {
		return "/front/coupon";
	}
	
	@RequestMapping(value = "helpLogin")
	public String helpLogin(HttpServletRequest req) {
		return "/front/helpLogin";
	}
	
	@RequestMapping(value = "helpMain")
	public String helpMain() {
		return "/front/helpMain";
	}



	@RequestMapping(value = "helpYh")
	public String helpYh(HttpServletRequest req) {
		HttpSession session = req.getSession();
		String help = (String) session.getAttribute("helpStreet");
		if(StringUtil.isBlank(help)) {
			return  "/front/helpLogin";
		}
		return "/front/helpYh";
	}
	
	@RequestMapping(value = "helpError")
	public String helpError(HttpServletRequest req) {
		HttpSession session = req.getSession();
		String help = (String) session.getAttribute("helpStreet");
		if(StringUtil.isBlank(help)) {
			return  "/front/helpLogin";
		}
		return "/front/helpError";
	}
	
	@RequestMapping(value = "helpLeave")
	public String helpLeave(HttpServletRequest req) {
		HttpSession session = req.getSession();
		String help = (String) session.getAttribute("helpStreet");
		if(StringUtil.isBlank(help)) {
			return  "/front/helpLogin";
		}
		return "/front/helpLeave";
	}
	
	@RequestMapping(value = "bycp")
	public String bycp() {
		return "/front/bycp";
	}
	
	@RequestMapping(value = "byxq")
	public String bycx() {
		return "/front/byxq";
	}
	@RequestMapping(value = "index")
	public String index() {
		return "/front/index";
	}
	/**
	 * ========================道路
	 * @return
	 */
	@RequestMapping(value = "r_index")
	public String r_index() {
		return "/front/road/r_index";
	}
	@RequestMapping(value = "r_prePay")
	public String r_prePay() {
		return "/front/road/r_prePay";
	}
	@RequestMapping(value = "r_logs")
	public String r_logs() {
		return "/front/road/r_logs";
	}
	@RequestMapping(value = "r_notice")
	public String r_notice() {
		return "/front/road/r_notice";
	}
	@RequestMapping(value = "r_orderDetail")
	public String r_orderDetail() {
		return "/front/road/r_orderDetail";
	}
	
	


}
